package cz.myslivec.slovicka.action.lesson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.richfaces.model.DataProvider;
import org.richfaces.model.ExtendedTableDataModel;
import org.richfaces.model.selection.Selection;
import org.richfaces.model.selection.SimpleSelection;

import cz.myslivec.slovicka.dao.LessonDao;
import cz.myslivec.slovicka.dao.VocableDao;
import cz.myslivec.slovicka.entity.Lesson;
import cz.myslivec.slovicka.entity.Vocable;

@Name("lessonEditAction")
@Scope(ScopeType.CONVERSATION)
public class LessonEditAction implements Serializable {
	private static final long serialVersionUID = -8949632267879233342L;

	@In
	EntityManager entityManager;

	@In
	LessonDao lessonDao;
	
	@In
	VocableDao vocableDao;

	@Logger
	Log log;
	
	private Selection selection = new SimpleSelection();
	
	@Out(required = false, scope = ScopeType.SESSION)
	@In(required = false)
	private Lesson selectedLesson;
	
	private List<Vocable> vocables; 

	@Out(required = false, scope = ScopeType.SESSION)
	@In(required = false)
	private Vocable selectedVocable;

	private boolean editMode = true;

	// INIT
	@Create
	public void init() {
		if(selectedLesson != null) {
			vocables = selectedLesson.getVocables();
		} else {
			vocables = new ArrayList<Vocable>();
		}
	}
	
	// TABLE
	public ExtendedTableDataModel<Vocable> getModel() {

		return new ExtendedTableDataModel<Vocable>(
				new DataProvider<Vocable>() {
					private static final long serialVersionUID = 6724923130388579437L;

					public Vocable getItemByKey(Object arg0) {
						Lesson rv = (Lesson) arg0;
						return vocableDao.get(rv.getId());
					}

					public List<Vocable> getItemsByRange(int arg0, int arg1) {
						return vocables.subList(arg0, arg1);
					}

					public Object getKey(Vocable vocable) {
						return vocable;
					}

					public int getRowCount() {
						return vocables.size();
					}
				});
	}

	public Selection getSelection() {
		if(selectedVocable != null && selectedVocable.getId() != null) {
			if(!selection.isSelected(selectedVocable)) {
				((SimpleSelection) selection).clear();
				((SimpleSelection) selection).addKey(selectedVocable);
			}
		}
		return selection;
	}
	
	public void setSelection(Selection selection) {
		this.selection = selection;
		selectedVocable = null;
		
		if (selection != null && selection.getKeys().hasNext()) {
			Object o = selection.getKeys().next();

			if (o instanceof Long) {
				selectedVocable = vocableDao.get((Long) o);
			} else if (o instanceof Vocable) {
				selectedVocable = (Vocable) o;
			}
			
		}
	}

	
	// METHODS
	public void test() {
		log.info("<>");
		log.info("<> TEST EXECUTED");
	}
	
	public void preview() {
		log.info("<>");
		log.info("<> PREVIEW EXECUTED");
	}
	
	@End(beforeRedirect = true)
	public String save() {
		// TODO
		log.info("<> SAVE EXECUTED");
		return "SAVED";
	}
	
	@End(beforeRedirect=true)
	public String endEdit() {
		// TODO
		log.info("<> endEDIT EXECUTED");
		return "OK";
	}
	
	// GETTERS & SETTERS
	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}

	public Lesson getSelectedLesson() {
		return selectedLesson;
	}

	public void setSelectedLesson(Lesson selectedLesson) {
		this.selectedLesson = selectedLesson;
	}

	public List<Vocable> getVocables() {
		return vocables;
	}

	public void setVocables(List<Vocable> vocables) {
		this.vocables = vocables;
	}
	
}
